<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Goods;
use App\Image;

class GoodsController extends Controller
{
    //商品列表页的展示
    public function index(Request $request)
    {
    	//获取请求的参数
    	$goods = Goods::where(function($query) use ($request){
    		// $query->where('goods.id', '=','images.goods_id')->get();
    		//检测参数
    		if(!empty($request->title)){
    			$query->where('title','like','%'.$request->title.'%');
    		}

    		if(!empty($request->size)){
    			$query->where('size',$request->size);
    		}
    	})->orderBy('id','asc')->paginate($request->input('num',5));

    	//读取商品信息
    	return view('admin.goods.index',['goods'=>$goods,'request'=>$request]);
    }


    //显示商品的添加
    public function create()
    {
    	return view('admin.goods.create');
    }

    public function store(Request $request)
    {
    	//表单验证
    	$this->validate($request,[
	    	'title' => 'required',
			'price' => 'required',
			'oldprice' => 'required',
			'kucun' => 'required',
			'color' => 'required',
			'size' => 'required',
			'detail'=>'required'
		],[
			'title.required'=>'商品名称不能为空',
			'price.required'=>'商品价格不能为空',
			// 'price.regex'=>'商品价格格式不正确',
			'oldprice.required'=>'商品原价不能为空',
			// 'oldprice.regex'=>'商品原价格式不正确',
			'kucun.required'=>'商品库存不能为空',
			'kucun.regex'=>'商品库存格式不正确',
			'color.required'=>'商品颜色不能为空',
			'size.required'=>'商品尺寸不能为空',
			'detail.required'=>'商品详情不能为空'
		]);

    	//创建模型对象
    	$goods=new Goods;
    	$goods->title=$request->title;
    	$goods->price=$request->price;
    	$goods->oldprice=$request->oldprice;
    	$goods->kucun=$request->kucun;
    	$goods->color=implode(',',$request->color);
    	$goods->size=implode(',',$request->size);
    	$goods->detail=$request->detail;

    	if($goods->save()){
    		$data=[];

    		if($request->hasFile('img')){
    			//上传图片
    			foreach($request->img as $k=>$v){
    				//检测文件是否可用
    				if($v->isValid()){
    					//上传文件
    					$dirname='./Uploads/'.date('Ymd').'/';
    					//获取文件的后缀
    					$suffix=$v->getClientOriginalExtension();

    					$name=time().rand(1000000,9999999).'.'.$suffix;

    					$v -> move($dirname,$name);

    					//获取图片的路径
    					$src = trim($dirname.$name,'.');

    					$image = new Image(['src'=>$src]);
    					$data[]= $image;
    				}
    			}

    			$res = $goods->image()->saveMany($data);

    			if($res){
    				return redirect('/goods')->with('success','添加成功');
    			}else{
    				return back()->with('error','添加失败');
    			}
    		}


    	}else{
    		return back()->with('error','添加失败');
    	}
    }

    //商品的修改
    public function edit($id)
    {
    	$goods = Goods::findOrFail($id);

    	// dd($goods);die;

    	//显示模板
    	return view('admin.goods.edit',['goods'=>$goods]); 

    }

    public function update(Request $request,$id)
    {
    	$goods=Goods::findOrFail($id);

    	//提取内容

    	$goods->title=$request->title;
    	$goods->price=$request->price;
    	$goods->oldprice=$request->oldprice;
    	$goods->kucun=$request->kucun;
    	$goods->color=implode(',',$request->color);
    	$goods->size=implode(',',$request->size);
    	$goods->detail=$request->detail;

    	if($goods->save()){
    		$data=[];

    		if($request->hasFile('img')){
    			//上传图片
    			foreach($request->img as $k=>$v){
    				//检测文件是否可用
    				if($v->isValid()){
    					//上传文件
    					$dirname='./Uploads/'.date('Ymd').'/';
    					//获取文件的后缀
    					$suffix=$v->getClientOriginalExtension();

    					$name=time().rand(1000000,9999999).'.'.$suffix;

    					$v -> move($dirname,$name);

    					//获取图片的路径
    					$src = trim($dirname.$name,'.');

    					$image = new Image(['src'=>$src]);
    					$data[]= $image;
    				}
    			}

    			$res = $goods->image()->saveMany($data);

    			if($res){
    				return redirect('/goods')->with('success','修改成功');
    			}else{
    				return back()->with('error','修改失败');
    			}
    		}


    	}else{
    		return back()->with('error','修改失败');
    	}

    }


    public function destroy($id)
    {

    	$goods=Goods::findOrFail($id);

    	if($goods->delete()){
    		return back()->with('success','执行成功');
    	}else{
    		return back()->with('error','执行失败');
    	}
    }





}
